package com.itheima.controller;


import com.itheima.entity.Result;
import com.itheima.pojo.Comment;
import com.itheima.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author wangchang
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/comment")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class CommentController {
    @Autowired
    private CommentService commentService;
    
    // 用户增加评论
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        String addResult = commentService.add(comment);
        if ("failed".equals(addResult)) {
            return new Result("error", "插入失败");
        }
        return new Result("success","插入成功");
    }
    
    // 用户删除评论
    @RequestMapping("/delete")
    public Result delete(@RequestBody Comment comment) {
        String deleteResult = commentService.delete(comment);
        if ("failed".equals(deleteResult)) {
            return new Result("error", "删除评论失败");
        }
        return new Result("success","删除评论成功");
    }
    
    // 删除选中评论
    @RequestMapping("/deleteBatch")
    public Result delete(@RequestBody List<Comment> commentList) {
        String deleteResult = commentService.deleteBatch(commentList);
        if ("failed".equals(deleteResult)) {
            return new Result("error", "删除选中评论失败");
        }
        return new Result("success","删除选中评论成功");
    }
    
    // 编辑评论
    @RequestMapping("/edit")
    public Result eidt(@RequestBody Comment comment) {
        String editResult = commentService.edit(comment);
        if ("failed".equals(editResult)) {
            return new Result("error", "编辑评论成功");
        }
        return new Result("success","编辑评论失败");
    }
    
    // 获取评论列表
    @RequestMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> searchMap) {
        Map<String,Object> map = commentService.getPageList(searchMap);
        com.itheima.entity.Page page = new com.itheima.entity.Page();
        // 设置返回的分页对象
        page.setCurrent((Integer) map.get("pageNum"));
        page.setRecords((List<Comment>) map.get("list"));
        page.setTotal((Integer) map.get("total"));
        return new Result("success", page);
    }
    
    //// 获取评论列表
    //@RequestMapping("/getList")
    //public Result getList(@RequestBody Map<String, Object> searchMap) {
    //    PageInfo<Comment> pageInfo = commentService.getPageList(searchMap);
    //    com.itheima.entity.Page page = new com.itheima.entity.Page();
    //    // 设置返回的分页对象
    //    page.setCurrent(pageInfo.getPageNum());
    //    page.setRecords(pageInfo.getList());
    //    page.setTotal((int) pageInfo.getTotal());
    //    return new Result("success", page);
    //}
    
}

